import sys
import os

# 添加src目录到Python路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/src')

from src.config.settings import settings
from src.ui.gradio_interface import GradioInterface
import logging

# 配置日志
logger = logging.getLogger(__name__)

def main():
    """主函数，启动应用
    
    初始化Gradio界面并启动Web服务
    """
    try:
        # 打印启动信息
        logger.info("=== 启动自然语言转SQL查询工具 ===")
        logger.info(f"当前配置: 数据库URI={settings.get_db_uri()}, Ollama模型={settings.get_ollama_config()['model']}")
        
        # 创建并启动Gradio界面
        interface = GradioInterface()
        demo = interface.create_interface()
        
        # 启动Web服务
        demo.launch(
            server_name="0.0.0.0",  # 允许外部访问
            server_port=7861,       # 修改为可用端口
            share=False             # 离线环境不分享
        )
        
    except Exception as e:
        logger.error(f"应用启动失败: {str(e)}", exc_info=True)
        print(f"应用启动失败: {str(e)}")
        sys.exit(1)

if __name__ == "__main__":
    main()